
package com.hd.sup.modules.vehicle.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.ValueUtils;
import com.hd.sup.modules.vehicle.model.VehicleOnOff;
import com.hd.sup.modules.vehicle.service.IVehicleOnOffService;
import com.hd.sup.modules.vehicle.wrapper.VehicleWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title VehicleDrivingController.java
 * @Description 车辆行驶统计控制器
 * @Package com.hd.sup.modules.vehicle.controller
 * @date 2020/12/31 11:37 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/vehicleDriving")
public class VehicleDrivingController extends BaseController{


    @Resource
    private IVehicleOnOffService vehicleOnOffService;

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 查询车辆行使统计信息表格
     * @date 2020/12/31 11:37 上午
     * @params [requestData]
     */
    @PostMapping("/list")
    public Result<?> vehicleDrivingList(@RequestBody RequestData requestData) {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String platNo = requestData.getString("platNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        String regionCode = requestData.getString("regionCode");
        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        Integer onlineStatus = ValueUtils.parseInteger(requestData.getString("onlineStatus"));
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return Result.error("参数范围时间和起始时间不允许同时选择");
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");

        VehicleOnOff param = new VehicleOnOff(DateUtil.fillDate(startDateStr, "start"), DateUtil.fillDate(endDateStr, "end"), dateRange, enterpriseNo, platNo, enterpriseName, regionCode, onlineStatus, sort, order);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleWrapper(vehicleOnOffService.getVehicleDrivingPage(page, param)).warp();
        page.setRecords(result);
        return Result.success(page);
    }

}
